package com.practice.HashMap为什么是线程不安全的;
import java.util.HashMap;
import java.util.Map;
/**
 * @program: tenghw-practice-demo
 * @author: tenghw
 * @create: 2024-07-18 20:14
 * @Version 1.0
 * @description: 扩容操作的问题
 **/
public class Demo2 {
    public static void main(String[] args) throws InterruptedException {
        Map<String, Integer> map = new HashMap<>(2,0.75f);
        map.put("key1", 1);
        map.put("key2", 2);
        map.put("key3", 3);

        Thread thread1 = new Thread(() -> {
            for(int i=4;i<1000;i++){
                map.put("key"+i,i);
            }
        });
        Thread thread2 = new Thread(() -> {
            for(int i=4;i<1000;i++){
                map.put("key"+i,i*2);
            }
        });

        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();

        //for(String key : map.keySet()){
        //    System.out.println(key + "=" + map.get(key));
        //}
        // 只是查看元素个数，而不是数组的长度
        System.out.println("size= " + map.size());
    }
}